package com.peenongsaban.freelance.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.peenongsaban.freelance.bean.RegisterBean;
import com.peenongsaban.freelance.bean.UserBean;
import com.peenongsaban.freelance.connection.DatabaseConnectionPool;
import com.peenongsaban.freelance.dao.LoginDAO;
import com.peenongsaban.freelance.meta.HistoryMeta;
import com.peenongsaban.freelance.meta.RegisterMeta;
import com.peenongsaban.freelance.meta.UserMeta;
import com.peenongsaban.freelance.meta.ViewMeta;
import com.peenongsaban.freelance.utils.ConstantUtils;
import com.peenongsaban.freelance.utils.ExceptionUtil;
import com.peenongsaban.freelance.utils.NetworkUtils;

public class LoginDAOImpl implements LoginDAO{

	@Override
	public RegisterBean getUser(String username, String password) {
		RegisterBean bean = null;
		DatabaseConnectionPool databaseConnection = new DatabaseConnectionPool();
		Connection conn = databaseConnection.getConnection();
		PreparedStatement statement = null;
		String sql = "SELECT * FROM "+ViewMeta.LOGIN_V+" WHERE "+RegisterMeta.COL_USERNAME+" = ? AND "+RegisterMeta.COL_PASSWORD+" = ? ";
		try {
			statement = conn.prepareStatement(sql);
			statement.setString(1, username);
			statement.setString(2, password);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				bean = new RegisterBean();
				bean.setUsername(rs.getString(RegisterMeta.COL_USERNAME));	
				bean.setEnable(rs.getBoolean(RegisterMeta.COL_IS_ENABLE));
				
				UserBean user = new UserBean();
				user.setIdCard(rs.getString(UserMeta.COL_ID_CARD));
				user.setName_th(rs.getString(UserMeta.COL_NAME_TH));
				user.setLastName_th(rs.getString(UserMeta.COL_LASTNAME_TH));
				user.setName_en(rs.getString(UserMeta.COL_NAME_EN));
				user.setLastName_en(rs.getString(UserMeta.COL_LASTNAME_EN));
				user.setPicture(rs.getString(UserMeta.COL_PICTURE));
				user.setUserStatus(rs.getString(UserMeta.COL_USER_STATUS));
				user.setAlias(rs.getString(UserMeta.COL_ALIAS));
				bean.setUser(user);
				
				//TODO add history login
				//TODO encrypt password
			}
			
			if(bean!=null){
				String ipAddress = NetworkUtils.getIPAddress();
				String macAddress = NetworkUtils.getMacAddress();
				String hostName = NetworkUtils.getHostName();
				conn.setAutoCommit(false);
				statement = null;
				String insertSql = "INSERT INTO "
						+ HistoryMeta.TABLE + "("
						+ HistoryMeta.COL_USERNAME + ", "
						+ HistoryMeta.COL_NOTE + ", "
						+ HistoryMeta.COL_DATE_TIME + ", "
						+ HistoryMeta.COL_IP_ADDRESS + ", "
						+ HistoryMeta.COL_MAC_ADDRESS + ", "
						+ HistoryMeta.COL_HOST_NAME + ", "
						+ HistoryMeta.COL_HISTORY_TYPEID + ") "
						+ "VALUES(?,?,SYSDATE(),?,?,?,?)";
				
				statement = conn.prepareStatement(insertSql);
				statement.setString(1, bean.getUsername());
				statement.setString(2, ConstantUtils.LOGIN_NOTE);
				statement.setString(3, ipAddress);
				statement.setString(4, macAddress);
				statement.setString(5, hostName);
				statement.setString(6, ConstantUtils.LOGIN_HISTORY_TYPE_ID);
				statement.execute();
				conn.commit();
			}
			
			
		} catch (Exception e) {
			ExceptionUtil.messageException(new Throwable(), e);
		} finally {
			databaseConnection.destroy();
			databaseConnection = null;
			try {
				statement.close();
				conn.close();
			} catch (Exception ex) {
				ExceptionUtil.messageException(new Throwable(), ex);
			}
		}
		
		return bean;
	}

}
